/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Search;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 *
 * @author Nuf
 */
public class SearchResultsRelevance {

    //private File qrelFile;
    private static ArrayList<ElementRelevance> results;
    private float fiveFirstResultsRelevance;
    private float tenFirstResultsRelevance;
    private float twentyFiveFirstResultsRelevance;

    public SearchResultsRelevance(String queryFilePath, ArrayList<ElementRelevance> results) throws IOException {
        this.results = results;
        fiveFirstResultsRelevance = 0;
        tenFirstResultsRelevance = 0;
        twentyFiveFirstResultsRelevance = 0;
        parseQrel(queryFilePath);
    }

    private void parseQrel(String queryFilePath) throws FileNotFoundException, IOException {
        InputStream flux = new FileInputStream(queryFilePath);
        InputStreamReader lecture = new InputStreamReader(flux);
        BufferedReader buff = new BufferedReader(lecture);
        String[] qrel;
        String line;
        String currentDoc, currentXpath;

        while ((line = buff.readLine()) != null) {
            qrel = line.split("	");
            // If the paragraph is relevant
            if (Integer.valueOf(qrel[2]) == 1) {
                // we get the document name (minus the "/Collection/" part, common to all names)
                currentDoc = (qrel[0].substring(11, qrel[0].length())).trim();
                // we get its xpath
                currentXpath = qrel[1];
                // we check if it's in our results
                for (int i = 0; i < 25; i++) {
                    boolean sameDoc = results.get(i).getDocument().compareToIgnoreCase(currentDoc) == 0;
                    boolean sameXpath = results.get(i).getElement().compareToIgnoreCase(currentXpath) == 0;
                    // if it's the same paragraph
                    if (sameDoc && sameXpath) {
                        // We increment the percentage of the different statistics
                        if (i < 5) {
                            fiveFirstResultsRelevance += 1;
                        }
                        if (i < 10) {
                            tenFirstResultsRelevance += 1;
                        }
                        twentyFiveFirstResultsRelevance += 1;
                    }

                }
            }
        }
        fiveFirstResultsRelevance *= 100 / 5f;
        tenFirstResultsRelevance *= 100 / 10f;
        twentyFiveFirstResultsRelevance *= 100 / 25f;
        buff.close();
    }

    public String toString() {
        return "Relevance out of 5 : " + fiveFirstResultsRelevance + "%\n"
                + "Relevance out of 10 : " + tenFirstResultsRelevance + "%\n"
                + "Relevance out of 25 : " + twentyFiveFirstResultsRelevance + "%";
    }
}
